package com.nettest.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

public class CommonUtil {
	public static boolean isEmptyText(String s) {
		if (s == null) {
			return true;
		}
		if ("".equals(s.trim())) {
			return true;
		}
		return false;
	}
	
	public static void clearCollection(Collection<?> c) {
		if (c != null) {
			c.clear();
			c = null;
		}
	}
	
	public static boolean isNull(Object obj) {
		if (obj == null || "".equals(obj.toString()) 
				|| "null".equals(obj.toString())) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean isExtNull(Object obj) {
		if (isNull(obj)) {
			return true;
		}

		if (obj instanceof String) {
			String str = (String) obj;
			if ("".equals(str.trim())) {
				return true;
			}
		} else if (obj instanceof Collection) {
			Collection<?> c = (Collection<?>) obj;
			if (c.isEmpty()) {
				return true;
			}
		} else if (obj instanceof Map) {
			Map<?,?> map = (Map<?,?>) obj;
			if (map.isEmpty()) {
				return true;
			}
		} else if (obj instanceof Iterator) {
			Iterator<?> iter = (Iterator<?>) obj;
			if (!iter.hasNext()) {
				return true;
			}
		} else if (obj.getClass().isArray()) {
			Object[] objArr = (Object[]) obj;
			if (objArr.length <= 0) {
				return true;
			}
		} else {
			if ("".equals(obj.toString().trim())) {
				return true;
			}
		}

		return false;
	}	
	
	public static int[] transformToFiscalYearMonth(int year, int month) {
		int[] fiscalYearMonth = new int[2];
		if (month <= 3) {
			fiscalYearMonth[0] = year-1;
			fiscalYearMonth[1] = 9+month;
		} else {
			fiscalYearMonth[0] = year;
			fiscalYearMonth[1] = month-3;
		}
		return fiscalYearMonth;
	}
	
	public static Map<String,Object> parseDelimitedTextToMap(String text, String itemDelimit, String keyValueDelimit) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (text == null || "".equals(text.trim())) {
			return resultMap;
		}
		
		String s = text.trim();
		String[] items = s.split(itemDelimit);
		for (String item : items) {
			item = item.trim();
			String[] keyValue = item.split(keyValueDelimit);
			resultMap.put(keyValue[0].trim(), keyValue[1].trim());
		}
		return resultMap;
	}
	
	public static String transformTextFromCodeToName(String text, String itemDelimit, String keyValueDelimit, Map<String, String> codeNameMap) {
		Map<String, Object> codeMap = parseDelimitedTextToMap(text, itemDelimit, keyValueDelimit);
		Map<String, Object> nameMap = transformCodeMapToNameMap(codeMap, codeNameMap);
		return buildDelimitedText(nameMap, ",", ":");
	}
	
	public static Map<String,Object> transformCodeMapToNameMap(Map<String, Object> codeMap, Map<String, String> codeNameMap) {
		Map<String, Object> nameMap = new HashMap<String, Object>();
		
		Iterator<String> iter = codeMap.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			Object value = codeMap.get(key);
			String name = codeNameMap.get(key);
			if (name != null) {
				key = name;
			}
			nameMap.put(key, value);
		}
		return nameMap;
	}
	    	
	public static String substractNumberKeyValuePairedItems(String items1, String items2) {
		Map<String, Object> itemMap1 = parseDelimitedTextToMap(items1, ",", ":");
		Map<String, Object> itemMap2 = parseDelimitedTextToMap(items2, ",", ":");
		if (itemMap1 == null || itemMap1.isEmpty()) {
			return items1;
		}
		if (itemMap2 == null || itemMap2.isEmpty()) {
			return items1;
		}
		
		Iterator<String> iter = itemMap1.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			if (itemMap2.containsKey(key)) {
				Double v1 = Double.parseDouble(itemMap1.get(key).toString());
				Double v2 = Double.parseDouble(itemMap2.get(key).toString());
				itemMap1.put(key, v1-v2);
			} 
		}
		
		String result=null;
		iter = itemMap1.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			if (result == null) {
			    result = result + key + ":" + itemMap1.get(key).toString();
			} else {
				result = result + "," + key + ":" + itemMap1.get(key).toString();
			}
		}
		return result;
	}
		
	
	public static String buildDelimitedText(Map<String, Object> textItemMap, String itemDelimit, String keyValueDelimit) {
		String result = "";
		
		if (textItemMap == null) {
			return "";
		}
		
		Iterator<String> iter = textItemMap.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			if (textItemMap.containsKey(key)) {
				Object objValue = textItemMap.get(key);
				String value = (objValue==null? "" : objValue.toString());
				if ("".equals(result)) {
					result = key + keyValueDelimit + value;
				} else {
					result = result + itemDelimit + key + keyValueDelimit + value;
				}
			}	
		}
		return result;
	}
	
	/**
	 * 生成随机密码
	 * 
	 * @param passLenth
	 *            生成的密码长度
	 * @return 随机密码
	 */
	public static String getRandomPassword(int passLenth) {
		StringBuffer buffer = new StringBuffer(
				"0123456789abcdefghijklmnopqrstuvwxyz");
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		int range = buffer.length();
		for (int i = 0; i < passLenth; i++) {
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
}
